package com.youi.shop.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.youi.shop.mapper.ProductCategoryMapper;
import com.youi.shop.pojo.entity.ProductCategory;
import com.youi.shop.pojo.vo.productcategory.ProductCategoryVO;
import com.youi.shop.service.ProductCategoryService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 商品类别表 服务实现类
 * </p>
 *
 * @author xxf
 * @since 2020-11-09
 */
@Service
public class ProductCategoryServiceImpl extends ServiceImpl<ProductCategoryMapper, ProductCategory> implements ProductCategoryService {

    @Resource
    ProductCategoryMapper productCategoryMapper;

    /**
     * 查询所有商品类别
     *
     * @return
     */
    @Override
    public List<ProductCategoryVO> selectAll() {
        QueryWrapper<ProductCategory> wrapper = new QueryWrapper<>();
        wrapper.eq("delete_status", 0);
        //查询所有
        List<ProductCategory> productCategoryList = productCategoryMapper.selectList(wrapper);
        List<ProductCategoryVO> list = new ArrayList<ProductCategoryVO>();
        //复制到VO对象返回集合
        for (ProductCategory productCategory : productCategoryList) {
            ProductCategoryVO categoryVO = new ProductCategoryVO();
            BeanUtils.copyProperties(productCategory, categoryVO);
            list.add(categoryVO);
        }
        return list;
    }

    @Override
    public List<ProductCategoryVO> selectByPage(int page, int size) {
        Page<ProductCategory> categoryPage = new Page<>(page, size);
//        List<ProductCategory> categoryList = (List<ProductCategory>) productCategoryMapper.selectPage(categoryPage, null);
        List<ProductCategory> categoryList = productCategoryMapper.selectPage(categoryPage, null).getRecords();
        List<ProductCategoryVO> list = new ArrayList<>();
        for (ProductCategory category : categoryList) {
            ProductCategoryVO categoryVO = new ProductCategoryVO();
            BeanUtils.copyProperties(category, categoryVO);
            list.add(categoryVO);
        }
        return list;
    }
}
